package com.study.listenter;

import com.study.bean.Student;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.startup.Tomcat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.HandlerMapping;

import java.util.List;

/**
 * @Description TODO
 * @Author v_yangliu
 * @Date 2022/1/11 14:22
 * @Version 1.0
 **/
@Slf4j
public class TestApplicationListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        log.info("==========我执行了onApplicationEvent方法===============");
        Object obj = applicationEvent.getSource();
        if (obj instanceof DispatcherServlet) {
            DispatcherServlet servlet = (DispatcherServlet) obj;
            List<HandlerMapping> adapters = servlet.getHandlerMappings();
            for(HandlerMapping mapping:adapters){
                log.info("mapping:{}",mapping);
            }
        }
        if (obj instanceof AnnotationConfigServletWebServerApplicationContext) {
            AnnotationConfigServletWebServerApplicationContext context = (AnnotationConfigServletWebServerApplicationContext) obj;
            context.register(Student.class);
            log.info("我注册了student类");
        }

        if (obj instanceof TomcatWebServer) {
            TomcatWebServer server = (TomcatWebServer) obj;
            int port = server.getPort();
            Tomcat tomcat = server.getTomcat();
            log.info("我启动的端口是{},类是{}", port, tomcat);
        }
       /* if (obj instanceof SpringApplication) {
            SpringApplication application = (SpringApplication) obj;
            WebApplicationType webType = application.getWebApplicationType();
            log.info("我启动的web类型是{}", webType.name());
        }*/
    }
}
